package com.fish.web.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fish.common.constant.Constant;
import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.finance.po.Settlement;
import com.fish.service.finance.SettlementService;

public class SettlementAction extends BaseAction {
	private static final long serialVersionUID = -4020478609911502107L;
	private static final Log loger = LogFactory.getLog(SettlementAction.class);
	private SettlementService settlementService;
	private Settlement settlement = new Settlement();
	private List<Settlement> settlementList = new ArrayList<Settlement>();
	private QueryPage queryPage = new QueryPage();

	public String goToSettlement() {
		return SUCCESS;
	}

	public String goToSettlementList() {
		if(!getSessionUser().isAdmin()){
			settlement.setSettleUser(getSessionUser().getUserId());
		}
		
		long count = settlementService.querySettlementCountByObject(settlement);
		if(count > 0){
			queryPage = new QueryPage(count, Constant.DEFAULT_PAGE_SIZE, queryPage.getCurPage());
		}
		
		settlementList = settlementService.querySettlementByObject(settlement,queryPage);
		return SUCCESS;
	}

	public String viewSettlementInfo() {
		if (StringUtils.isNotBlank(settlement.getSettlementId())) {
			settlement = settlementService.querySettlementByPrimaryKey(settlement);
		}
		return SUCCESS;
	}

	public String saveSettlement() {
		try {
			if (StringUtils.isBlank(settlement.getSettlementId())) {
				settlement.setSettlementId(this.getNewUUID());
				settlement.setCreateTime(new Date());
				settlement.setUpdateTime(new Date());
				settlementService.addSettlement(settlement);
			} else {
				settlement.setUpdateTime(new Date());
				settlementService.updateSettlement(settlement);
			}
		} catch (Exception e) {
			loger.debug("保存订单有错！");
			loger.debug(e);
		}

		return goToSettlementList();
	}

	/**
	 * @return the settlement
	 */
	public Settlement getSettlement() {
		return settlement;
	}

	/**
	 * @return the settlementList
	 */
	public List<Settlement> getSettlementList() {
		return settlementList;
	}

	/**
	 * @param settlementService
	 *            the settlementService to set
	 */
	public void setSettlementService(SettlementService settlementService) {
		this.settlementService = settlementService;
	}

	/**
	 * @param settlement
	 *            the settlement to set
	 */
	public void setSettlement(Settlement settlement) {
		this.settlement = settlement;
	}

	public QueryPage getQueryPage() {
		return queryPage;
	}

	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}

}